import CommonConstant from "../../../../constant/CommonConstant";

export default class PasswordText extends Laya.Script3D {

    private numMaterail: Array<Laya.BlinnPhongMaterial>;

    private mowner: Laya.Sprite3D;

    public Text: string;

    public Init() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.Text = ""
        this.numMaterail = new Array<Laya.BlinnPhongMaterial>();

        for (let index = 0; index < this.mowner.numChildren; index++) {
            let child = this.mowner.getChildAt(index) as Laya.MeshSprite3D
            let material: Laya.BlinnPhongMaterial = new Laya.BlinnPhongMaterial();
            this.setTexture(material, CommonConstant._numTexturePath + "transparent.png", Laya.Handler.create(this, () => {
                material.albedoColorA = 0.5; //透明度
                material.blend = Laya.RenderState.BLEND_ENABLE_ALL;
                material.blendSrc = Laya.RenderState.BLENDPARAM_SRC_ALPHA;
                material.blendDst = Laya.RenderState.BLENDPARAM_ONE_MINUS_SRC_ALPHA;
                material.renderQueue = Laya.BaseMaterial.RENDERQUEUE_TRANSPARENT;
                material.depthTest = Laya.RenderState.DEPTHTEST_ALWAYS;
            }))
            child.meshRenderer.material = material;
            this.numMaterail.push(material);
        }
    }

    private setTexture(material: Laya.BlinnPhongMaterial, texturePath: string, callBack: Laya.Handler = null) {
        Laya.Texture2D.load(texturePath, Laya.Handler.create(this, function (texture: Laya.Texture2D): void {
            material.albedoTexture = texture;
            if (callBack != null) {
                callBack.run();
            }
        }));
    }

    public SetPasswordTexture(password: string): void {
        this.Text = password;

        let passwordLength = password.length;
        for (let index = 0; index < this.numMaterail.length; index++) {
            if (index < passwordLength) {
                let num = password[index]
                let numInt = parseInt(num);
                this.setTexture(this.numMaterail[index], CommonConstant._numTexturePath + numInt + ".png", null);
            } else {
                this.setTexture(this.numMaterail[index], CommonConstant._numTexturePath + "transparent.png", null);
            }
        }
    }

    public ClearPasswordTexture(): void {
        this.Text = "";
        for (let index = 0; index < this.numMaterail.length; index++) {
            this.setTexture(this.numMaterail[index], CommonConstant._numTexturePath + "transparent.png", null);
        }
    }
}